package com.umeox.calculator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.Vector;


class HistoryAdapter extends BaseAdapter {
    private Vector<HistoryEntry> mEntries;
    private Logic mEval;
    private LayoutInflater mInflater;

    HistoryAdapter(Context context, History history, Logic evaluator) {
        this.mEntries = history.mEntries;

        this.mInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
        this.mEval = evaluator;
    }

    public int getCount() {
        return this.mEntries.size() - 1;
    }

    public Object getItem(int position) {
        return this.mEntries.elementAt(position);
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public boolean hasStableIds() {
        return true;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = this.mInflater.inflate(R.layout.history_item, parent, false);
        } else {
            view = convertView;
        }
        TextView result = (TextView) view.findViewById(R.id.historyResult);
        HistoryEntry entry = this.mEntries.elementAt(position);
        String base = entry.getBase();
        ((TextView) view.findViewById(R.id.historyExpr)).setText(entry.getBase());
        try {
            result.setText("= " + this.mEval.evaluate(base));
        } catch (Exception e) {
            result.setText("");
        }
        return view;
    }
}
